//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections;
using System.Web.UI;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Localization;
using DotNetNuke.UI.WebControls;
using DNNControls = DotNetNuke.UI.WebControls;
namespace DotNetNuke.Common.Lists
{
	public partial class ListEditor : PortalModuleBase
	{
		private enum eImageType
		{
			Folder = 0,
			Page = 1
		}
		protected string Mode
		{
			get { return lstEntries.Mode; }
			set { lstEntries.Mode = value; }
		}
		private void BindList(string key)
		{
			lstEntries.SelectedKey = key;
			if (PortalSettings.ActiveTab.IsSuperTab)
			{
				lstEntries.ListPortalID = Null.NullInteger;
			}
			else
			{
				lstEntries.ListPortalID = this.PortalId;
			}
			lstEntries.ShowDelete = true;
			lstEntries.DataBind();
		}
		private void BindTree()
		{
			ListController ctlLists = new ListController();
			ListInfoCollection colLists = ctlLists.GetListInfoCollection();
			Hashtable indexLookup = new Hashtable();
			DNNtree.TreeNodes.Clear();
			foreach (ListInfo list in colLists)
			{
				DNNControls.TreeNode node = new DNNControls.TreeNode(list.DisplayName);
				{
					node.Key = list.Key;
					node.ToolTip = list.EntryCount.ToString() + " entries";
					node.ImageIndex = (int)eImageType.Folder;
				}
				if (list.Level == 0)
				{
					DNNtree.TreeNodes.Add(node);
				}
				else
				{
					if (indexLookup[list.ParentList] != null)
					{
						DNNControls.TreeNode parentNode = (DNNControls.TreeNode)indexLookup[list.ParentList];
						parentNode.TreeNodes.Add(node);
					}
				}
				if (indexLookup[list.Key] == null)
				{
					indexLookup.Add(list.Key, node);
				}
			}
		}
		private DNNControls.TreeNode GetParentNode(string ParentKey)
		{
			int i;
			for (i = 0; i <= DNNtree.TreeNodes.Count - 1; i++)
			{
				if (DNNtree.TreeNodes[i].Key == ParentKey)
				{
					return DNNtree.TreeNodes[i];
				}
			}
			return null;
		}
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			lstEntries.ID = "ListEntries";
		}
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			DNNtree.NodeClick += new DnnTree.DNNTreeNodeClickHandler(DNNTree_NodeClick);
			cmdAddList.Click += new EventHandler(cmdAddList_Click);

			try
			{
				if (!Page.IsPostBack)
				{
					DNNtree.ImageList.Add(ResolveUrl("~/images/folder.gif"));
					DNNtree.ImageList.Add(ResolveUrl("~/images/file.gif"));
					DNNtree.IndentWidth = 10;
					DNNtree.CollapsedNodeImage = ResolveUrl("~/images/max.gif");
					DNNtree.ExpandedNodeImage = ResolveUrl("~/images/min.gif");
					if (Request.QueryString["Key"] != null)
					{
						Mode = "ListEntries";
						BindList(Request.QueryString["Key"]);
					}
					else
					{
						Mode = "NoList";
					}
					BindTree();
				}
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);

			if (Mode == "NoList")
			{
				dshDetails.Visible = false;
				divDetails.Visible = false;
			}
			else
			{
				dshDetails.Visible = true;
				divDetails.Visible = true;
				dshDetails.Text = Localization.GetString(Mode, this.LocalResourceFile);
			}
		}
		private void DNNTree_NodeClick(object source, DotNetNuke.UI.WebControls.DNNTreeNodeClickEventArgs e)
		{
			Mode = "ListEntries";
			BindList(e.Node.Key);
		}
		private void cmdAddList_Click(object sender, System.EventArgs e)
		{
			Mode = "AddList";
			BindList("");
		}
	}
}
